package com.css.fxfzysx.modules.vaolEngineering.service.impl;

import com.css.fxfzysx.constants.FxfzConstants;
import com.css.fxfzysx.modules.cztDataMange.result.ParsingResult;
import com.css.fxfzysx.modules.lifeline.annotation.ValidationProcessor;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxAirReceiverEntity;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxMainTableEntity;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxVersionEntity;
import com.css.fxfzysx.modules.lifeline.repository.LifelineEngineeringRepository;
import com.css.fxfzysx.modules.lifeline.utils.ShpUtils;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.param.update.AirSupplyParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.SgManagementRepository;
import com.css.fxfzysx.modules.vaolEngineering.service.SgManagementService;
import com.css.fxfzysx.modules.vaolEngineering.utils.MainTableUtils;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.ShpAnalyticUtil;
import com.css.fxfzysx.util.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SgManagementServiceImpl implements SgManagementService {

    @Autowired
    private SgManagementRepository sgManagementRepository;

    @Autowired
    private ShpUtils shpUtils;

    @Autowired
    private MainTableUtils mainTableUtils;

    @Autowired
    private LifelineEngineeringRepository lifelineEngineeringRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void calculation(String versionId, String tableId) {
        List<SmxAirReceiverEntity> list = new ArrayList<>();
        if (PlatformObjectUtils.isNotEmpty(tableId)) {
            SmxAirReceiverEntity entity = sgManagementRepository.getByTableId(tableId);
            list.add(entity);
        } else {
            list = sgManagementRepository.getByVersionId(versionId);
        }

        if (PlatformObjectUtils.isNotEmpty(list)) {
            list.forEach(it -> {
                BigDecimal result1 = it.getRailLength()
                        .multiply(it.getThickness1().pow(2))
                        .multiply(it.getYieldStress())
                        .divide(new BigDecimal(4).multiply(it.getRailHeight()), 4, RoundingMode.HALF_UP);
                it.setResult1(result1);
                if (PlatformObjectUtils.isEmpty(it.getResult1Correct())) {
                    it.setResult1Correct(result1);
                }

                BigDecimal result2 = new BigDecimal(2)
                        .multiply(it.getWeldWidth())
                        .multiply(it.getWeldHeight())
                        .multiply(it.getWeldStress())
                        .multiply(it.getDamagedWeldLength());
                it.setResult2(result2);
                if (PlatformObjectUtils.isEmpty(it.getResult2Correct())) {
                    it.setResult2Correct(result2);
                }

                BigDecimal r = it.getAxisRadius().pow(3);
                BigDecimal result3 = (new BigDecimal(Math.PI).multiply(r)
                        .multiply(it.getYieldStress1())
                        .multiply(it.getUltimateCapacity())
                        .divide(new BigDecimal(4).multiply(it.getCalculatedLength()), 4, RoundingMode.HALF_UP));
                it.setResult3(result3);
                if (PlatformObjectUtils.isEmpty(it.getResult3Correct())) {
                    it.setResult3Correct(result3);
                }

                BigDecimal result4 = new BigDecimal(2)
                        .multiply(it.getBottomPlateLength())
                        .multiply(it.getWeldingWidth())
                        .multiply(it.getWeldStress());
                it.setResult4(result4);
                if (PlatformObjectUtils.isEmpty(it.getResult4Correct())) {
                    it.setResult4Correct(result4);
                }

                sgManagementRepository.saveAirReceiverResult(it);
                String year = mainTableUtils.getKeyByValue("smxjsnd", MainTableUtils.getYear(it.getYear()));
                DestructiveStateVo stateVo = sgManagementRepository.getByBasicInfo(it,year);
                mainTableUtils.updateCalculationStatus(it.getTableId(),stateVo.getDisasterRate6State(),stateVo.getDisasterRate7State(),stateVo.getDisasterRate8State(),stateVo.getDisasterRate9State(),stateVo.getDisasterRate10State(), 1);
            });
        }
        mainTableUtils.updateVersionCalculationStatus(versionId, 0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> uploadAirReceiver(String fileIds, String versionId, String userId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        shpUtils.uniformFileName(fileIds);
        ParsingResult shpData = shpUtils.getShpData(fileIds);
        //是否成功
        if (!shpData.getMessage().equals("ok")) {
            map.put("res", false);
            map.put("mess", shpData.getMessage());
        }
        List<List<Object>> shpList = shpData.getShpList();
        List<SmxAirReceiverEntity> airReceiverList = new ArrayList<>();
        for (int i = 0; i < shpList.size(); i++) {
            String id = UUIDGenerator.getUUID();
            List<Object> list = shpList.get(i);
            if (list.size() < 42) {
                return null;
            }
            SmxAirReceiverEntity entity = new SmxAirReceiverEntity();
            entity.setDelFlag("0");
            entity.setId(UUIDGenerator.getUUID());
            entity.setTableId(id);
            entity.setCreateUser(userId);
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());

            if (!PlatformObjectUtils.isEmpty(list.get(0))) {
                entity.setGeom(list.get(0).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(1))) {
                entity.setAirReceiverId(ShpAnalyticUtil.getStringVal(list.get(1).toString(), "编码", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(2))) {
                entity.setName(ShpAnalyticUtil.getStringVal(list.get(2).toString(), "编码", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(3))) {
                entity.setLongitude(ShpAnalyticUtil.getStringVal(list.get(3).toString(), "经度", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(4))) {
                entity.setLatitude(ShpAnalyticUtil.getStringVal(list.get(4).toString(), "纬度", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(5))) {
                entity.setYear(ShpAnalyticUtil.getIntegerVal(list.get(5).toString(), "建设年份", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(6))) {
                entity.setStyle(ShpAnalyticUtil.getStringVal(list.get(6).toString(), "型号", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(7))) {
                entity.setCapacity(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(7).toString(), "公称容积", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(8))) {
                entity.setPressure(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(8).toString(), "工作压力", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(9))) {
                entity.setInnerDiameter(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(9).toString(), "筒体内径", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(10))) {
                entity.setHeight(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(10).toString(), "筒体高度", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(11))) {
                entity.setThickness(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(11).toString(), "筒体、封头壁厚", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(12))) {
                entity.setWeight(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(12).toString(), "参考重量", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(13))) {
                entity.setMaterial(ShpAnalyticUtil.getStringVal(list.get(13).toString(), "结构材料", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(14))) {
                entity.setStructuralStyle(ShpAnalyticUtil.getStringVal(list.get(14).toString(), "结构形式", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(15))) {
                entity.setRailLength(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(15).toString(), "导轨长度", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(16))) {
                entity.setRailHeight(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(16).toString(), "导轨高度", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(17))) {
                entity.setThickness1(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(17).toString(), "导轨中腹厚度", true)));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(18))) {
                entity.setBaseType(ShpAnalyticUtil.getStringVal(list.get(18).toString(), "基础形式", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(19))) {
                entity.setSite(ShpAnalyticUtil.getStringVal(list.get(19).toString(), "场地类别", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(20))) {
                entity.setFortification(ShpAnalyticUtil.getStringVal(list.get(20).toString(), "设防情况", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(21))) {
                entity.setMaterialType(list.get(21).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(22))) {
                entity.setElasticityModulus(new BigDecimal(list.get(22).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(23))) {
                entity.setYieldStress(new BigDecimal(list.get(23).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(24))) {
                entity.setPermissibleStress(new BigDecimal(list.get(24).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(25))) {
                entity.setLiquefaction6(list.get(25).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(26))) {
                entity.setLiquefaction7(list.get(26).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(27))) {
                entity.setLiquefaction8(list.get(27).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(28))) {
                entity.setLiquefaction9(list.get(28).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(29))) {
                entity.setSeismicSubsidence6(list.get(29).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(30))) {
                entity.setSeismicSubsidence7(list.get(30).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(31))) {
                entity.setSeismicSubsidence8(list.get(31).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(32))) {
                entity.setSeismicSubsidence9(list.get(32).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(33))) {
                entity.setIntensity(list.get(33).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(34))) {
                entity.setWeldWidth(new BigDecimal(list.get(34).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(35))) {
                entity.setWeldHeight(new BigDecimal(list.get(35).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(36))) {
                entity.setWeldStress(new BigDecimal(list.get(36).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(37))) {
                entity.setDamagedWeldLength(new BigDecimal(list.get(37).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(38))) {
                entity.setAxisRadius(new BigDecimal(list.get(38).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(39))) {
                entity.setYieldStress1(new BigDecimal(list.get(39).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(40))) {
                entity.setUltimateCapacity(new BigDecimal(list.get(40).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(41))) {
                entity.setCalculatedLength(new BigDecimal(list.get(41).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(42))) {
                entity.setBottomPlateLength(new BigDecimal(list.get(42).toString()));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(43))) {
                entity.setWeldingWidth(new BigDecimal(list.get(43).toString()));
            }
            if (list.size() > 44 && !PlatformObjectUtils.isEmpty(list.get(44))) {
                entity.setExtends1(list.get(44).toString());
            }
            if (list.size() > 45 && !PlatformObjectUtils.isEmpty(list.get(45))) {
                entity.setExtends2(list.get(45).toString());
            }
            if (list.size() > 46 && !PlatformObjectUtils.isEmpty(list.get(46))) {
                entity.setExtends3(list.get(46).toString());
            }
            if (list.size() > 47 && !PlatformObjectUtils.isEmpty(list.get(47))) {
                entity.setExtends4(list.get(47).toString());
            }
            if (list.size() > 48 && !PlatformObjectUtils.isEmpty(list.get(48))) {
                entity.setExtends5(list.get(48).toString());
            }
            if (list.size() > 49 && !PlatformObjectUtils.isEmpty(list.get(49))) {
                entity.setExtends6(list.get(49).toString());
            }
            if (list.size() > 50 && !PlatformObjectUtils.isEmpty(list.get(50))) {
                entity.setExtends7(list.get(50).toString());
            }
            if (list.size() > 51 && !PlatformObjectUtils.isEmpty(list.get(51))) {
                entity.setExtends8(list.get(51).toString());
            }
            if (list.size() > 52 && !PlatformObjectUtils.isEmpty(list.get(52))) {
                entity.setExtends9(list.get(52).toString());
            }
            if (list.size() > 53 && !PlatformObjectUtils.isEmpty(list.get(53))) {
                entity.setExtends10(list.get(53).toString());
            }
            entity.setVersionId(versionId);
            airReceiverList.add(entity);

            SmxMainTableEntity mainEntity = new SmxMainTableEntity();
            mainEntity.setId(id);
            mainEntity.setCreateUser(userId);
            mainEntity.setYear(entity.getYear());
            mainEntity.setFortification(entity.getFortification());
            mainEntity.setBusinessObject(FxfzConstants.CZT_SMX_RQ);
            mainEntity.setVersionId(versionId);
            mainTableUtils.saveMainTable(mainEntity);
        }
        boolean res = false;
        int i = 1;
        for (SmxAirReceiverEntity entity : airReceiverList) {
            entity.setSerialNumber(i);
            res = sgManagementRepository.saveAirReceiver(entity);
            i += 1;
        }
        map.put("res", res);
        return map;
    }

    @Override
    public HashMap<Integer, ArrayList<String>> getDataValidation(String versionId) {
        HashMap<Integer, ArrayList<String>> map = new HashMap<>();
        SmxVersionEntity versionEntity = lifelineEngineeringRepository.getById(versionId);
        if (PlatformObjectUtils.isNotEmpty(versionEntity)) {
            List<SmxAirReceiverEntity> airReceiverList = sgManagementRepository.getByVersionId(versionEntity.getId());
            int i = 1;
            for (SmxAirReceiverEntity entity : airReceiverList) {
                ArrayList<String> list = ValidationProcessor.validate(entity);
                if (list.size() != 0) {
                    map.put(i, list);
                }
                i += 1;
            }
        }
        return map;
    }

    @Override
    public SmxAirReceiverEntity getByTableId(String id) {
        return sgManagementRepository.getByTableId(id);
    }

    @Override
    public void updateAirSupplyById(AirSupplyParam airSupplyParam) {
        sgManagementRepository.updateAirSupplyById(airSupplyParam);
    }

    @Override
    public AirSupplyParam count(AirSupplyParam param) {
        BigDecimal result1 = param.getRailLength()
                .multiply(param.getThickness1().pow(2))
                .multiply(param.getYieldStress())
                .divide(new BigDecimal(4).multiply(param.getRailHeight()), 4, RoundingMode.HALF_UP);
        param.setResult1(result1);
        if (PlatformObjectUtils.isEmpty(param.getResult1Correct())) {
            param.setResult1Correct(result1);
        }

        BigDecimal result2 = new BigDecimal(2)
                .multiply(param.getWeldWidth())
                .multiply(param.getWeldHeight())
                .multiply(param.getWeldStress())
                .multiply(param.getDamagedWeldLength());
        param.setResult2(result2);
        if (PlatformObjectUtils.isEmpty(param.getResult2Correct())) {
            param.setResult2Correct(result2);
        }

        BigDecimal r = param.getAxisRadius().pow(3);
        BigDecimal result3 = (new BigDecimal(Math.PI).multiply(r)
                .multiply(param.getYieldStress1())
                .multiply(param.getUltimateCapacity())
                .divide(new BigDecimal(4).multiply(param.getCalculatedLength()), 4, RoundingMode.HALF_UP));
        param.setResult3(result3);
        if (PlatformObjectUtils.isEmpty(param.getResult3Correct())) {
            param.setResult3Correct(result3);
        }

        BigDecimal result4 = new BigDecimal(2)
                .multiply(param.getBottomPlateLength())
                .multiply(param.getWeldingWidth())
                .multiply(param.getWeldStress());
        param.setResult4(result4);
        if (PlatformObjectUtils.isEmpty(param.getResult4Correct())) {
            param.setResult4Correct(result4);
        }
        return param;
    }
}
